﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

using WhereIsMyCash.XML;

namespace WhereIsMyCash.Web
{
    public partial class _TestXMLHelper : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var xmldoc = XMLHelper.xmlDoc("XMLFile1.xml".ToFilePath());
            ShowXML(xmldoc);
            this.ltlXPath.Text = XMLHelper.GetXmlNodeByXpath("XMLFile1.xml".ToFilePath(),"//root//name").InnerText;
        }

        protected void AddNode(object sender, EventArgs e)
        {
            string addInnerText = "<font color=\"green\">New Item</font>";
            var xmldoc = XMLHelper.xmlDoc("XMLFile1.xml".ToFilePath());
            
            if (xmldoc != null)
            {
                XmlParameter[] xmlpars = { new XmlParameter("sth1", addInnerText, true), new XmlParameter("sth2", addInnerText) };
                XMLHelper.AddNewNode(xmldoc, "root", xmlpars);
                try
                {
                    xmldoc.Save("XMLFile1.xml".ToFilePath());
                }
                catch
                { 
                    
                }
            }

            ShowXML(xmldoc);
        }

        void ShowXML(XmlDocument xmldoc)
        {
            if (xmldoc != null)
            {
                var line = string.Empty;
                foreach (XmlNode xmlNode in xmldoc.DocumentElement)
                {
                    line += "<lable>" + xmlNode.Name + "：</lable>" + xmlNode.InnerText + "<br/>";
                }
                this.ltlXMLItems.Text = line;
            }
        }
    }
}
